package com.data.modules.online.cgreport.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.DialectFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.DialectModel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.IDialect;
import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import com.data.common.system.vo.DynamicDataSourceModel;
import com.data.common.util.ReflectHelper;
import com.data.common.util.dynamic.db.DataSourceCachePool;
import com.data.common.util.dynamic.db.DynamicDBUtil;
import com.data.modules.online.cgform.util.CgformUtil;
import com.data.modules.online.config.executor.ConditionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CgReportSqlUtil {

    public static final String f390a = " where ";

    public static final String f391b = " and ";

    public static final String f392c = " or ";

    private static final Logger f393d = LoggerFactory.getLogger(CgReportSqlUtil.class);

    public static String replaceParamsForSQL(String sql) {
        String replaceAll = sql.replaceAll("(?i) where ", f390a).replaceAll("(?i) and ", f391b).replaceAll("(?i) or ", " or ");
        Matcher matcher = Pattern.compile("(,\\s*|\\s*(\\w|\\.)+\\s*[^, ]+ *\\S*)\\$\\{\\w+\\}\\S*").matcher(replaceAll);
        while (matcher.find()) {
            String group = matcher.group();
            if (group.indexOf(f390a) != -1) {
                replaceAll = replaceAll.replace(group, group.substring(0, group.indexOf(f390a)) + " where 1=1");
            } else if (group.indexOf(f391b) != -1) {
                String substring = group.substring(group.indexOf("and"));
                if (substring.indexOf("(") > 0) {
                    replaceAll = replaceAll.replace(substring.substring(substring.indexOf("(") + 1), " 1=1 ");
                } else {
                    replaceAll = replaceAll.replace(substring, "and 1=1");
                }
            } else if (group.indexOf(" or ") != -1) {
                String substring2 = group.substring(group.indexOf("or"));
                if (substring2.indexOf("(") > 0) {
                    replaceAll = replaceAll.replace(substring2.substring(substring2.indexOf("(") + 1), " 1=1 ");
                } else {
                    replaceAll = replaceAll.replace(substring2, "or 1=1");
                }
            } else if (group.startsWith(CgformUtil.f231E)) {
                replaceAll = replaceAll.replace(group, " ,1 ");
            } else {
                replaceAll = replaceAll.replace(group, " 1=1 ");
            }
        }
        return replaceAll.replaceAll("(?i)\\(\\s*1=1\\s*(AND|OR)", "(").replaceAll("(?i)(AND|OR)\\s*1=1", "");
    }

    public static Map<String, Object> analysisRequestParam(HttpServletRequest httpServletRequest) {
        String str;
        Map<String,String[]> parameterMap = httpServletRequest.getParameterMap();
        HashMap hashMap = new HashMap();
        String str2 = "";
        for (Map.Entry<String,String[]> entry : parameterMap.entrySet()) {
            String str3 = (String) entry.getKey();
            Object value = entry.getValue();
            if ("_t".equals(str3) || value == null) {
                str = "";
            } else if (value instanceof String[]) {
                String[] strArr = (String[]) value;
                for (int i = 0; i < strArr.length; i++) {
                    str2 = strArr[i] + CgformUtil.f231E;
                }
                str = str2.substring(0, str2.length() - 1);
            } else {
                str = value.toString();
            }
            hashMap.put(str3, str);
            str2 = str;
        }
        return hashMap;
    }

    public static boolean isSelectSql(String str) {
        return str.toLowerCase().indexOf("select") == 0;
    }

    public static String formatSqlStr(String str) {
        return String.format("SELECT COUNT(1) \"total\" FROM ( %s ) temp_count", str);
    }

    public static Map<String, Object> getSqlFiledForStrs(String str, String str2) {
        IDialect dialect = DialectFactory.getDialect(JdbcUtils.getDbType(DataSourceCachePool.getCacheDynamicDataSourceModel(str).getDbUrl()));
        Page page = new Page(1, 1);
        DialectModel buildPaginationSql = dialect.buildPaginationSql(str2, page.offset(), page.getSize());
        String dialectSql = buildPaginationSql.getDialectSql();
        if (!dialectSql.contains("?")) {
            return (Map) DynamicDBUtil.findOne(str, dialectSql, new Object[0]);
        }
        long longValue = ((Long) ReflectHelper.getFieldVal("firstParam", buildPaginationSql)).longValue();
        long longValue2 = ((Long) ReflectHelper.getFieldVal("secondParam", buildPaginationSql)).longValue();
        if (dialectSql.length() - dialectSql.replaceAll("\\?", "").length() == 1) {
            return (Map) DynamicDBUtil.findOne(str, dialectSql, new Object[]{Long.valueOf(longValue)});
        }
        return (Map) DynamicDBUtil.findOne(str, dialectSql, new Object[]{Long.valueOf(longValue), Long.valueOf(longValue2)});
    }

    public static List<Map<String, Object>> m348a(String str, String str2, String str3, int i, int i2, Map<String, Object> map) {
        DialectModel dialectModel;
        DynamicDataSourceModel cacheDynamicDataSourceModel = DataSourceCachePool.getCacheDynamicDataSourceModel(str2);
        if (!Boolean.valueOf(str).booleanValue()) {
            IDialect dialect = DialectFactory.getDialect(JdbcUtils.getDbType(cacheDynamicDataSourceModel.getDbUrl()));
            Page page = new Page((long) i, (long) i2);
            DialectModel buildPaginationSql = dialect.buildPaginationSql(str3, page.offset(), page.getSize());
            str3 = buildPaginationSql.getDialectSql();
            dialectModel = buildPaginationSql;
        } else {
            dialectModel = null;
        }
        if (str3.contains("?")) {
            long longValue = ((Long) ReflectHelper.getFieldVal("firstParam", dialectModel)).longValue();
            long longValue2 = ((Long) ReflectHelper.getFieldVal("secondParam", dialectModel)).longValue();
            if (str3.length() - str3.replaceAll("\\?", "").length() == 1) {
                str3 = ConditionHandler.m370a(str3, longValue);
            } else {
                str3 = ConditionHandler.m371a(str3, longValue, longValue2);
            }
        }
        return DynamicDBUtil.findListByNamedParam(str2, str3, map);
    }


    public static void main(String[] args) {
        System.out.println(replaceParamsForSQL("select * from sys_user where id   ='${id}' and del_flag=  ${flag}"));
    }


}
